package com.heima.comment.service.impl;

import com.heima.apis.wemedia.IWmHandClient;
import com.heima.comment.repository.CommentRepository;
import com.heima.comment.service.CommentRepayService;
import com.heima.model.comment.dtos.CommentRepayDto;
import com.heima.model.comment.dtos.CommentRepayLikeDto;
import com.heima.model.comment.dtos.CommentRepaySaveDto;
import com.heima.model.comment.pojos.Comment;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentRepayServiceImpl implements CommentRepayService {
    @Autowired
    private IWmHandClient wmHandClient;
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    //二级评论保存
    @Override
    public ResponseResult save(CommentRepaySaveDto dto) {
        if (null==dto||null==dto.getContent()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //长度判断
        if (dto.getContent().length() > 140) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //文本垃圾检测
        if (!wmHandClient.handle(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        Comment comment = new Comment();
        //设置一级评论id
        comment.setParentId(dto.getCommentId());
        //设置用户id
        comment.setUserId(AppThreadLocalUtil.getUser().getId());
        //设置评论内容
        comment.setContent(dto.getContent());
        //设置点赞
        comment.setLikes(0);
        //设置回复
        comment.setReply(0);
        //创建修改时间
        comment.setCreatedTime(new Date().getTime());
        comment.setUpdatedTime(new Date().getTime());
        //存入DB数据库
        commentRepository.save(comment);
          //更新回复数
        Comment comment1 = mongoTemplate.findById(dto.getCommentId(), Comment.class);
        Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(dto.getCommentId()));  //_id区分引号 "1"和1
            Update update = Update.update("reply",comment1.getReply()+1 );
            mongoTemplate.upsert(query, update, "comment");  //有则更新，没有则新增
        System.out.println("获取到保存的文章评论" + comment);
        return ResponseResult.okResult(comment);
    }
    //二级评论查询
    @Override
    public ResponseResult load(CommentRepayDto dto) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(AppThreadLocalUtil.getUser().getId()))
                .addCriteria(Criteria.where("createdTime").lt(dto.getMinDate().getTime()))
                .addCriteria(Criteria.where("parentId").is(dto.getCommentId()))
                .with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        System.out.println(commentList);
        return ResponseResult.okResult(commentList);
    }
    //二级评论点赞
    @Override
    public ResponseResult like(CommentRepayLikeDto dto) {
        Comment comment = mongoTemplate.findById(dto.getCommentRepayId(), Comment.class);
        Integer userId = AppThreadLocalUtil.getUser().getId();
        if (null == comment.getLikes()) {
            comment.setLikes(0);
        }
        if ("0".equals(dto.getOperation())) {
            comment.setLikes(comment.getLikes() + 1);
            comment.setOperation(0);
        } else {
            comment.setLikes(comment.getLikes() - 1);
            comment.setOperation(1);
        }
        commentRepository.save(comment);
        return ResponseResult.okResult(comment.getLikes());
    }
}
